package com.java110.mall.cart.storeOrderEngine;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.storeOrder.StoreOrderUserRoleDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.intf.user.IWuyeV1InnerServiceSMO;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.user.PropertyOwnerDto;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName StoreOrderUserEngine
 * @Description TODO
 * @Author wuxw
 * @Date 2022/5/21 0:36
 * @Version 1.0
 * add by wuxw 2022/5/21
 **/

@Component
public class StoreOrderUserEngine {

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;

    @Autowired
    private IWuyeV1InnerServiceSMO wuyeV1InnerServiceSMOImpl;


    public StoreOrderUserRoleDto queryStoreOrderUser(StoreOrderPo storeOrderPo, JSONArray goodsList, JSONObject param) {

        StoreOrderUserRoleDto storeOrderUserDto = null;
        String shopId = goodsList.getJSONObject(0).getString("shopId");
        //说明来自 线上
        if (!"-1".equals(storeOrderPo.getPersonId())) {
            storeOrderUserDto = getStoreOrderUserRoleByOnline(storeOrderPo.getPersonId(), shopId);
        } else {
            storeOrderUserDto = getStoreOrderUserRoleByOffline(param, shopId);
        }

        return storeOrderUserDto;
    }

    /**
     * 线下交易
     *
     * @param param
     * @param shopId
     * @return
     */
    private StoreOrderUserRoleDto getStoreOrderUserRoleByOffline(JSONObject param, String shopId) {
        StoreOrderUserRoleDto tmpStoreOrderUserDto = new StoreOrderUserRoleDto();
        tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_COMMON);
        tmpStoreOrderUserDto.setShopId(shopId);
        JSONObject vipuser = param.getJSONObject("vipUsers");
        if (null == vipuser || vipuser.size() < 1) {
            return tmpStoreOrderUserDto;
        }
        //查询会员是否存在
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipId(vipuser.getString("vipId"));
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        Assert.listOnlyOne(shopVipUserDtos, "会员不存在");

        tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_VIP);


        PropertyOwnerDto propertyOwnerDto = new PropertyOwnerDto();
        propertyOwnerDto.setOwnerTel(shopVipUserDtos.get(0).getVipTel());
        PropertyOwnerDto tmpPropertyOwnerDto = wuyeV1InnerServiceSMOImpl.getOwner(propertyOwnerDto);
        if (tmpPropertyOwnerDto == null) {
            return tmpStoreOrderUserDto;
        }

        //查询 是否业主
        tmpStoreOrderUserDto.setCommunityId(tmpPropertyOwnerDto.getCommunityId());
        tmpStoreOrderUserDto.setOwnerId(tmpPropertyOwnerDto.getOwnerId());
        tmpStoreOrderUserDto.setStoreId(tmpPropertyOwnerDto.getStoreId());
        tmpStoreOrderUserDto.setStoreName(tmpPropertyOwnerDto.getStoreName());
        if (StoreOrderUserRoleDto.ROLE_VIP.equals(tmpStoreOrderUserDto.getRole())) {//说明是业主也是vip
            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_OWNER_VIP);
        } else {
            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_OWNER);
        }
        return tmpStoreOrderUserDto;
    }

    /**
     * 线上交易
     *
     * @param personId
     * @param shopId
     * @return
     */
    public StoreOrderUserRoleDto getStoreOrderUserRoleByOnline(String personId, String shopId) {
        UserDto userDto = new UserDto();
        userDto.setUserId(personId);
        List<UserDto> userDtoList = userV1InnerServiceSMOImpl.queryUsers(userDto);
        Assert.listOnlyOne(userDtoList, "线上交易，但是不存在用户信息");

        StoreOrderUserRoleDto tmpStoreOrderUserDto = BeanConvertUtil.covertBean(userDtoList.get(0), StoreOrderUserRoleDto.class);
        tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_COMMON);

        //查询店铺会员是否存在 如果存在 不添加 add by wuxw
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipTel(userDtoList.get(0).getTel());
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        if (!ListUtil.isNull(shopVipUserDtos)) {
            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_VIP);
        }


        PropertyOwnerDto propertyOwnerDto = new PropertyOwnerDto();
        propertyOwnerDto.setOwnerTel(userDtoList.get(0).getTel());
        PropertyOwnerDto tmpPropertyOwnerDto = wuyeV1InnerServiceSMOImpl.getOwner(propertyOwnerDto);
        if (tmpPropertyOwnerDto == null) {
            return tmpStoreOrderUserDto;
        }

        //查询 是否业主
        tmpStoreOrderUserDto.setCommunityId(tmpPropertyOwnerDto.getCommunityId());
        tmpStoreOrderUserDto.setCommunityName(tmpPropertyOwnerDto.getCommunityName());
        tmpStoreOrderUserDto.setOwnerId(tmpPropertyOwnerDto.getOwnerId());
        tmpStoreOrderUserDto.setStoreId(tmpPropertyOwnerDto.getStoreId());
        tmpStoreOrderUserDto.setStoreName(tmpPropertyOwnerDto.getStoreName());
        if (StoreOrderUserRoleDto.ROLE_VIP.equals(tmpStoreOrderUserDto.getRole())) {//说明是业主也是vip
            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_OWNER_VIP);
        } else {
            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_OWNER);
        }

        return tmpStoreOrderUserDto;
    }


    public boolean judgeUser(StoreOrderPo storeOrderPo, String shopId) {
        UserDto userDto = new UserDto();
        userDto.setUserId(storeOrderPo.getPersonId());
        List<UserDto> userDtoList = userV1InnerServiceSMOImpl.queryUsers(userDto);
        if (userDtoList == null || userDtoList.size() < 1) {
            return false;
        }

        //查询店铺会员是否存在 如果存在 不添加 add by wuxw
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipTel(userDtoList.get(0).getTel());
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        if (shopVipUserDtos != null && shopVipUserDtos.size() > 0) {
            return true;
        }
        return false;
    }

}
